//
//  Select_VisibleViewController.m
//  fxxiaoke
//
//  Created by 鞠超 on 16/9/14.
//  Copyright © 2016年 jc. All rights reserved.
//

#import "Select_VisibleViewController.h"

#import "Select_DirectlyleaderTableViewCell.h"
#import "Organized_systemViewController.h"
#import "Select_scopeViewController.h"

@interface Select_VisibleViewController ()  <UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating, UISearchControllerDelegate, UIScrollViewDelegate>

@property (nonatomic, strong) UITableView * tableView;

//tableview头视图
@property (nonatomic, strong) UIView * view_HeadView;
@property (nonatomic, strong) UISearchController * searchController;
@property (nonatomic, strong) UIView * view_Organization;
//
@property (nonatomic, assign) NSInteger number_section;
@property (nonatomic, assign) NSInteger number_row;
//顶部视图
@property (nonatomic, strong) UIView * view_Top;
@property (nonatomic, strong) UIView * view_Line;
@property (nonatomic, strong) UIButton * btn_Colleague;
@property (nonatomic, strong) UIButton * btn_department;
//底部视图
@property (nonatomic, strong) UIView * view_Bottom;
@property (nonatomic, strong) UILabel * lbl_selected;
@property (nonatomic, strong) UIButton * btn_BottomDepartment;
@property (nonatomic, strong) UIButton * btn_define;

@end

@implementation Select_VisibleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self p_navi];
    
    [self p_setupView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - navi
- (void )p_navi
{
    _btnLeft.hidden = YES;
    _lblLeft.hidden = YES;
    _btnRight.hidden = YES;
    _lblRight.hidden = YES;
    
    _lblTitle.text = @"选择可见范围";
    
    //左返回
    UILabel * lbl_return = [[UILabel alloc] initWithFrame:CGRectMake(15, 35, 50, 15)];
    lbl_return.text = @"取消";
    lbl_return.textColor = [UIColor whiteColor];
    lbl_return.font = [UIFont systemFontOfSize:17];
    [_topView addSubview:lbl_return];
    
    UIButton * btn_return = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_return.frame = CGRectMake(13, 22, 60, 40);
    [_topView addSubview:btn_return];
    [btn_return addTarget:self action:@selector(btn_returnAction:) forControlEvents:(UIControlEventTouchUpInside)];
}

//左返回
- (void )btn_returnAction:(UIButton *)sender
{
    //取消活跃状态
    self.searchController.active = 0;
    
    [self dismissViewControllerAnimated:YES completion:^{
    }];
}

//tabbar
- (void )viewWillAppear:(BOOL)animated
{
    [(AppDelegate *)[[UIApplication sharedApplication] delegate] hiddenTabBar];
}

#pragma mark - 布局
- (void )p_setupView
{
    self.view.backgroundColor = [UIColor whiteColor];
    //顶部视图
    [self p_TopView];
    //底部视图
    [self p_BottomView];
    //测试
    self.number_row = 2;
    self.number_section = 11;
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 104, SCREEN_WIDTH, SCREEN_HEIGHT - 144) style:(UITableViewStylePlain)];
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.sectionIndexBackgroundColor = [UIColor clearColor];
    self.tableView.sectionIndexColor = [UIColor blackColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableFooterView = [[UIView alloc] init];
    //头视图
    [self p_headView];
    
    [self.view addSubview:self.tableView];
    
    //注册
    [self.tableView registerClass:[Select_DirectlyleaderTableViewCell class] forCellReuseIdentifier:@"cell_UserList"];
}

#pragma mark - tableview代理
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.number_section;
}

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.number_row;
}

- (CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Select_DirectlyleaderTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell_UserList" forIndexPath:indexPath];
    cell.btn_Select.userInteractionEnabled = NO;
    
    
    cell.image_Head.image = [UIImage imageNamed:@"heads.jpg"];
    cell.user_Name.text = @"XXX(姓名)";
    cell.user_Department.text = @"销售经理-XX销售X部";
    
    return cell;
}

- (void )tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    //保存数据选中
}

//左边的
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSArray * arr_data = @[@"A",@"B",@"D",@"E",@"F",@"G",@"T",@"W",@"X",@"Y",@"Z"];
    
    if(self.searchController.active == 1)
    {
        return nil;
    }
    else
    {
        return arr_data[section];
    }
}

//右边的
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    if(self.searchController.active == 1)
    {
        return nil;
    }
    else
    {
        return @[@"A",@"B",@"D",@"E",@"F",@"G",@"T",@"W",@"X",@"Y",@"Z"];
    }
}

#pragma mark - scrollView代理
- (void )scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    if(self.searchController.active == 1)
    {
        [self.searchController.searchBar endEditing:YES];
    }
}

#pragma mark - 点击后跳到按组织查算界面
- (void )btn_OrganizationAction:(UIButton *)sender
{
    //    NSLog(@"点击后跳到按组织查算界面");
    
    Organized_systemViewController * organized_systemViewController = [[Organized_systemViewController alloc] init];
    organized_systemViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    
    [self presentViewController:organized_systemViewController animated:YES completion:^{
    }];
}

#pragma mark - 头视图
- (void )p_headView
{
    if(self.searchController.active == 1)
    {
        self.view_HeadView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
        self.tableView.tableHeaderView = self.view_HeadView;
        
        
        self.searchController.searchBar.frame = CGRectMake(0, 0, SCREEN_WIDTH, 44);
        self.searchController.searchBar.placeholder = @"搜姓名、手机号";
        self.searchController.dimsBackgroundDuringPresentation = NO;
        self.searchController.searchResultsUpdater = self;
        self.searchController.delegate = self;
        [self.view_HeadView addSubview:self.searchController.searchBar];
    }
    else
    {
        self.view_HeadView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 100)];
        self.tableView.tableHeaderView = self.view_HeadView;
        
        
        self.searchController.searchBar.frame = CGRectMake(0, 0, SCREEN_WIDTH, 44);
        self.searchController.searchBar.placeholder = @"搜姓名、手机号";
        self.searchController.dimsBackgroundDuringPresentation = NO;
        self.searchController.searchResultsUpdater = self;
        self.searchController.delegate = self;
        [self.view_HeadView addSubview:self.searchController.searchBar];
        
        //
        self.view_Organization = [[UIView alloc] initWithFrame:CGRectMake(0, 44, SCREEN_WIDTH, 56)];
        self.view_Organization.backgroundColor = [UIColor whiteColor];
        [self.view_HeadView addSubview:self.view_Organization];
        
        UILabel * lbl_Organization = [[UILabel alloc] initWithFrame:CGRectMake(15, 18, 200, 20)];
        lbl_Organization.text = @"按组织架构查看";
        lbl_Organization.textColor = [UIColor blackColor];
        lbl_Organization.font = [UIFont systemFontOfSize:17];
        lbl_Organization.textAlignment = NSTextAlignmentLeft;
        [self.view_Organization addSubview:lbl_Organization];
        
        UIImageView * image_Return = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 10 - 20, 18, 20, 20)];
        image_Return.userInteractionEnabled = YES;
        image_Return.image = [UIImage imageNamed:@"rigth-return.png"];
        [self.view_Organization addSubview:image_Return];
        
        UIButton * btn_Organization = [UIButton buttonWithType:(UIButtonTypeSystem)];
        btn_Organization.frame = CGRectMake(0, 0, SCREEN_WIDTH, 56);
        [self.view_Organization addSubview:btn_Organization];
        [btn_Organization addTarget:self action:@selector(btn_OrganizationAction:) forControlEvents:(UIControlEventTouchUpInside)];
    }
}

#pragma mark - UISearchController代理和update
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
    if([searchController.searchBar.text length] > 0)
    {
        self.number_section = 1;
        self.number_row = 3;
        
        dispatch_async(dispatch_get_main_queue(), ^{
            //刷新tableView(记住,要更新放在主线程中)
            [self.tableView reloadData];
        });
    }
}

- (void)didPresentSearchController:(UISearchController *)searchController
{
    self.number_section = 0;
    self.number_row = 0;
    
    [self p_headView];
    
    dispatch_async(dispatch_get_main_queue(), ^{
        //刷新tableView(记住,要更新放在主线程中)
        [self.tableView reloadData];
    });
}

- (void)didDismissSearchController:(UISearchController *)searchController
{
    self.number_section = 11;
    self.number_row = 2;
    
    [self p_headView];
    
    dispatch_async(dispatch_get_main_queue(), ^{
        //刷新tableView(记住,要更新放在主线程中)
        [self.tableView reloadData];
    });
}

#pragma mark - 懒加载搜索框控制器
- (UISearchController *)searchController
{
    if(_searchController == nil)
    {
        self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    }
    
    return _searchController;
}

#pragma mark - 顶部视图
- (void )p_TopView
{
    self.view_Top = [[UIView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, 40)];
    self.view_Top.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.view_Top];
    
    //同事
    self.btn_Colleague = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.btn_Colleague.frame = CGRectMake(0, 0, SCREEN_WIDTH / 2, 40);
    [self.btn_Colleague setTitle:@"选同事" forState:(UIControlStateNormal)];
    [self.btn_Colleague setTitleColor:[UIColor colorWithRed:250/255.0 green:176/255.0 blue:96/255.0 alpha:1.0f] forState:(UIControlStateNormal)];
    self.btn_Colleague.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.view_Top addSubview:self.btn_Colleague];
    [self.btn_Colleague addTarget:self action:@selector(btn_ColleagueAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    //选部门
    self.btn_department = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.btn_department.frame = CGRectMake(SCREEN_WIDTH / 2, 0, SCREEN_WIDTH / 2, 40);
    [self.btn_department setTitle:@"选部门" forState:(UIControlStateNormal)];
    [self.btn_department setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
    self.btn_department.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.view_Top addSubview:self.btn_department];
    [self.btn_department addTarget:self action:@selector(btn_departmentAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    //
    self.view_Line = [[UIView alloc] init];
    self.view_Line.frame = CGRectMake(0, 38, SCREEN_WIDTH / 2, 2);
    self.view_Line.backgroundColor = [UIColor colorWithRed:250/255.0 green:176/255.0 blue:96/255.0 alpha:1.0f];
    [self.view_Top addSubview:self.view_Line];
}

#pragma mark - 头部点击按钮
- (void )btn_ColleagueAction:(UIButton *)sender
{
    //    NSLog(@"点击选同事");
    if(self.view_Line.frame.origin.x != 0)
    {
        [self.btn_Colleague setTitleColor:[UIColor colorWithRed:250/255.0 green:176/255.0 blue:96/255.0 alpha:1.0f] forState:(UIControlStateNormal)];
        [self.btn_department setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
        [UIView animateWithDuration:0.4f animations:^{
            self.view_Line.frame = CGRectMake(0, 38, SCREEN_WIDTH / 2, 2);
        }];
    }
    
    dispatch_async(dispatch_get_main_queue(), ^{
        //刷新tableView(记住,要更新放在主线程中)
        [self.tableView reloadData];
    });
}

- (void )btn_departmentAction:(UIButton *)sender
{
    //    NSLog(@"点击选部门");
    if(self.view_Line.frame.origin.x == 0)
    {
        [self.btn_Colleague setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
        [self.btn_department setTitleColor:[UIColor colorWithRed:250/255.0 green:176/255.0 blue:96/255.0 alpha:1.0f] forState:(UIControlStateNormal)];
        [UIView animateWithDuration:0.4f animations:^{
            self.view_Line.frame = CGRectMake(SCREEN_WIDTH / 2, 38, SCREEN_WIDTH / 2, 2);
        }];
    }
    dispatch_async(dispatch_get_main_queue(), ^{
        //刷新tableView(记住,要更新放在主线程中)
        
        [self.tableView reloadData];
    });
}

#pragma mark - 底部视图
- (void )p_BottomView
{
    self.view_Bottom = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 40, SCREEN_WIDTH, 40)];
    self.view_Bottom.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view addSubview:self.view_Bottom];
    
    UIView * view_bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.30)];
    view_bottomLine.backgroundColor = [UIColor grayColor];
    [self.view_Bottom addSubview:view_bottomLine];
    
    //确定
    self.btn_define = [UIButton buttonWithType:(UIButtonTypeSystem)];
    self.btn_define.frame = CGRectMake(SCREEN_WIDTH - 80 - 5, 5, 80, 30);
    self.btn_define.backgroundColor = [UIColor colorWithRed:250/255.0 green:176/255.0 blue:96/255.0 alpha:1.0f];
    self.btn_define.layer.cornerRadius = 5;
    [self.btn_define setTitle:@"确定" forState:(UIControlStateNormal)];
    [self.btn_define setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [self.view_Bottom addSubview:self.btn_define];
    [self.btn_define addTarget:self action:@selector(btn_defineAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    //已选择
    self.lbl_selected = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 55, 20)];
    self.lbl_selected.text = @"已选择:";
    self.lbl_selected.font = [UIFont systemFontOfSize:15.0f];
    self.lbl_selected.textColor = [UIColor colorWithRed:250/255.0 green:176/255.0 blue:96/255.0 alpha:1.0f];
    [self.view_Bottom addSubview:self.lbl_selected];
    
    //点击后部门及同事
    self.btn_BottomDepartment = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.btn_BottomDepartment.frame = CGRectMake(CGRectGetMaxX(self.lbl_selected.frame) + 5, 5, SCREEN_WIDTH - CGRectGetMaxX(self.lbl_selected.frame) - 95, 30);
    [self.btn_BottomDepartment setTitleColor:[UIColor colorWithRed:250/255.0 green:176/255.0 blue:96/255.0 alpha:1.0f] forState:(UIControlStateNormal)];
    [self.btn_BottomDepartment setTitle:@"销售中心,XXX" forState:(UIControlStateNormal)];
    //文字靠在显示 *************************************************************
    self.btn_BottomDepartment.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    self.btn_BottomDepartment.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    self.btn_BottomDepartment.titleLabel.font = [UIFont systemFontOfSize:15.0f];
    [self.view_Bottom addSubview:self.btn_BottomDepartment];
    [self.btn_BottomDepartment addTarget:self action:@selector(btn_BottomDepartmentAction:) forControlEvents:(UIControlEventTouchUpInside)];
}

#pragma mark - 点击确定
- (void )btn_defineAction:(UIButton *)sender
{//    NSLog(@"点击确定");
    
    NSUserDefaults * userdefault = [NSUserDefaults standardUserDefaults];
    [userdefault setObject:@"3" forKey:@"record_Rows"];
    
    //取消活跃状态
    self.searchController.active = 0;
    [self dismissViewControllerAnimated:YES completion:^{
    }];
}

#pragma mark - 点击后部门及同事
- (void )btn_BottomDepartmentAction:(UIButton *)sender
{
//    NSLog(@"点击后模态出部门及同事");
    
    Select_scopeViewController * select_scopeViewController = [[Select_scopeViewController alloc] init];
    [self presentViewController:select_scopeViewController animated:YES completion:^{
    }];
}



















@end
